<?php
class ControllerDeploySubnet extends Controller {
        public function index(){

            if(isset($this->request->post['app_id'])){
                $this->data['app_id']=$this->request->post['app_id'];
            }else{
                $this->data['app_id']=0;
            }

            if(isset($this->request->post['vpc_name'])){
                $this->data['vpc_name']=$this->request->post['vpc_name'];
            }
            $this->data['region']=$this->request->post['region'];
            $this->data['subnet_name'] ="";
            $this->data['network'] ="";
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/subnet.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/deploy/subnet.tpl';
            } else {
                $this->template = 'default/template/deploy/subnet.tpl';
            }
            $this->response->setOutput($this->render());
         }
        public function creat_subnet(){

        $this->language->load('deploy/subnet');
        $json = array();
        // Validate if customer is logged in.
        if (!$this->customer->isLogged()) {
            $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
        }
        // Validate cart has products and has stock.
        if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
            $json['redirect'] = $this->url->link('checkout/cart');
        }
        // Validate subnet Name
        if (!$json) {
            if (!preg_match('/^[0-9a-zA-Z_]{3,32}$/i', $this->request->post['subnet_name'])) {
                $json['error']['subnet_name'] = $this->language->get('error_subnet_name');
            }

            if ($this->backend->check_subnet_name_exist($this->request->post['app_id'],$this->request->post['subnet_name'],$this->request->post['vpc_name'], $this->customer->getEmail(), $this->customer->getPassPlain())) {
                $json['error']['subnet_name'] = $this->language->get('error_subnet_name_exist');
            }

            if (!preg_match('/^((1|1[0-9]{0,2}|2[0-9]{0,1}|2[0-4][0-9]|25[0-5]|[3-9][0-9]{0,1}))$/i', $this->request->post['network'])) {
                $json['error']['network'] = $this->language->get('error_network');
            }

            if ($this->backend->check_subnet_address($this->request->post['app_id'],$this->request->post['vpc_name'],$this->request->post['network'], $this->customer->getEmail(), $this->customer->getPassPlain())) {
                $json['error']['network'] = $this->language->get('error_network_exist');
            }
        }
        // Update session
        if (!$json) {
            $this->session->data['app']['subnet'][$this->request->post['app_id']]=array(
                'vpc_name'  => trim($this->request->post['vpc_name']),
                'subnet_name'  => trim($this->request->post['subnet_name']),
                'network'  => trim($this->request->post['network']),
            );
            $json="success";
        }
        $this->response->setOutput(json_encode($json));

    }
        public function edit_subnet(){

        $this->language->load('deploy/subnet');
        $json = array();
        // Validate if customer is logged in.
        if (!$this->customer->isLogged()) {
            $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
        }
        // Validate cart has products and has stock.
        if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
            $json['redirect'] = $this->url->link('checkout/cart');
        }
        // Validate subnet Name
        if (!$json) {
            if (!preg_match('/^[0-9a-zA-Z_]{3,32}$/i', $this->request->post['subnet_name'])) {
                $json['error']['subnet_name'] = $this->language->get('error_subnet_name');
            }

            if ($this->backend->check_subnet_name_exist($this->request->post['app_id'],$this->request->post['subnet_name'],$this->request->post['vpc_name'], $this->customer->getEmail(), $this->customer->getPassPlain())) {
                $json['error']['subnet_name'] = $this->language->get('error_subnet_name_exist');
            }

            if (!preg_match('/^((1|1[0-9]{0,2}|2[0-9]{0,1}|2[0-4][0-9]|25[0-5]|[3-9][0-9]{0,1}))$/i', $this->request->post['network'])) {
                $json['error']['network'] = $this->language->get('error_network');
            }

            if ($this->backend->check_subnet_address($this->request->post['app_id'],$this->request->post['vpc_name'],$this->request->post['network'], $this->customer->getEmail(), $this->customer->getPassPlain())) {
                $json['error']['network'] = $this->language->get('error_network_exist');
            }
        }
        // Update session
        if (!$json) {
            $this->session->data['app']['subnet'][$this->request->post['app_id']]=array(
                'vpc_name'  => trim($this->request->post['vpc_name']),
                'subnet_name'  => trim($this->request->post['subnet_name']),
                'network'  => trim($this->request->post['network']),
            );
            $json="success";
        }
        $this->response->setOutput(json_encode($json));

    }
        public function check_subnet_address(){
            $vpcname=$this->request->post['vpc_name'];
            $subnetaddr=$this->request->post['subnetaddr'];
            $app_id=$this->request->post['app_id'];

            if(is_numeric($subnetaddr)&&$subnetaddr>0&&$subnetaddr<256){
                if(!$this->backend->check_subnet_address($app_id,$vpcname,$subnetaddr, $this->customer->getEmail(), $this->customer->getPassPlain())){
                    $json['result']="success";
                }else{
                    $json['result']="exist";
                }
            }else{
                $json['result']="exist";

            }
            $this->response->setOutput(json_encode($json));
        }
        public function check_subnet_name_exist(){

            $subnetname = $this->request->post['subnet_name'];
            $vpcname = $this->request->post['vpc_name'];
            if(!$this->backend->check_subnet_name_exist($this->request->post['app_id'],$subnetname,$vpcname, $this->customer->getEmail(), $this->customer->getPassPlain())){
                $json['result']="success";
            }else{
                $json['result']="exist";
            }
            $this->response->setOutput(json_encode($json));
    }
        public function edit(){

            if(isset($this->request->post['app_id'])){
                $app_id=$this->request->post['app_id'];
            }else{
                $app_id=0;
            }
            $this->data['edit']=true;
            $this->data['app_id']=$app_id;
            $this->data['vpc_name'] =$this->session->data['app']['subnet'][$app_id]['vpc_name'];
            $this->data['subnet_name'] =$this->session->data['app']['subnet'][$app_id]['subnet_name'];
            $this->data['network'] =$this->session->data['app']['subnet'][$app_id]['network'];
            if(isset($this->request->post['vpc_name'])){
                $this->data['vpc_name']=$this->request->post['vpc_name'];
            }

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/subnet.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/deploy/subnet.tpl';
        } else {
            $this->template = 'default/template/deploy/subnet.tpl';
        }
        $this->response->setOutput($this->render());
    }
}
